<template>
  <div>
    <MyTable :arr="list">
      <template #head>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{ row }">
        <td>{{ row.id }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>
          <input class="tag-input form-control" style="width: 100px" type="text" v-if="row.inputVisible" v-focus @blur="row.inputVisible = false" @keydown.enter="addBtn(row)" v-model="row.inputValue" @keydown.esc="row.inputValue = ''" />
          <button v-else style="display: block" class="btn btn-primary btn-sm add-tag" @click="row.inputVisible = true">+Tag</button>
          <span v-for="(item, index) in row.tags" :key="index" class="badge badge-warning">{{ item }}</span>
        </td>
        <td><button class="btn btn-danger btn-sm" @click="removeBtn(row.id)">删除</button></td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "./MyTable"
import axios from "axios"
axios.defaults.baseURL = "https://www.escook.cn"
export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    }
  },
  created() {
    axios({
      url: "/api/goods",
    }).then((res) => {
      console.log(res)
      this.list = res.data.data
    })
  },
  methods: {
    removeBtn(id) {
      this.list = this.list.filter((obj) => obj.id !== id)
    },
    addBtn(row) {
      if (row.inputValue.trim() === "") {
        return alert("请输入内容")
      }

      row.tags.push(row.inputValue)
      row.inputValue = ""
    },
  },
}
</script>
<style lang="less" scoped>
.badge {
  margin-right: 5px;
}
</style>
